package com.atguigu.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.InitVodObject;
import com.atguigu.vod.utils.VodPropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadAliyunVideo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String fileName=file.getOriginalFilename();//上传文件原始的名称
            String title=fileName.substring(0, fileName.lastIndexOf("."));//上传之后显示的名称,去除掉后缀mp4
            UploadStreamRequest request = new UploadStreamRequest(VodPropertiesUtil.ACCESS_KEY_ID, VodPropertiesUtil.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId=response.getVideoId();
            return videoId;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    //删除多个视频
    @Override
    public void removeAllVideoList(List<String> videoIdList) {
        try {
            //初始化对象
            DefaultAcsClient defaultAcsClient = InitVodObject.initVodClient(VodPropertiesUtil.ACCESS_KEY_ID, VodPropertiesUtil.ACCESS_KEY_SECRET);
            //初始化删除对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //将list集合转换为字符串，也就是"11,12,13"这样
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");//这个字符串以逗号分隔
            request.setVideoIds(videoIds);
            defaultAcsClient.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }
}
